package nu.mine.tbje.easyEDT;

import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import java.io.IOException;
import javax.faces.context.ResponseWriter;

import nu.mine.tbje.easyEDT.data.Cours;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
/***
 * Permet de dessiner l'emploi du temps pour la balise edtToday
 * @author Tbje
 *
 */
public class EdtTodayUIComp extends UIComponentBase {
	public void encodeBegin(FacesContext context) throws IOException
	{
		ResponseWriter writer = context.getResponseWriter();
		Iterator listecours = ((List)getAttributes().get("cours")).iterator();
		String date = (String)getAttributes().get("date");

		if (listecours != null) {
			boolean started = false;
			SimpleDateFormat d1 = new SimpleDateFormat("ddMMyyyy");
			Date dDate;
			try {
				dDate = d1.parse(date);
			} catch (ParseException e) {
				dDate = new Date();
			}
			writer.writeText(writeDate(dDate), null);
			while (listecours.hasNext()) {
				Cours cours = (Cours)listecours.next();
				SimpleDateFormat d2 = new SimpleDateFormat("dd/MM/yyyy");
				try {
					if (d2.parse(cours.getDate()).equals(dDate)) {
						if (!started){
							started = true;
							writer.startElement("table", this);
							writer.writeAttribute("style", "border-style: solid; border-width: 1px; width: 200px", null);		
						}
						writer.startElement("tr", this);
						writer.startElement("td", this);
						writer.writeAttribute("style", "background-color: lightgreen; text-align: center; padding: 5px; height: 80px", null);
						writer.startElement("b", this);
						writer.writeText(cours.getName(), null);
						writer.endElement("b");
						writer.write("<br/>");
						writer.writeText(cours.getRoom(), null);
						writer.write("<br/>");
						writer.writeText(cours.getTeacher(), null);
						writer.write("<br/>");
						writer.writeText(cours.getTime() + " - ", null);
						writer.writeText(cours.getDuration(), null);
						writer.endElement("td");   
						writer.endElement("tr");
					}
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (started){
				writer.endElement("table");      
			} else {
				writer.write("<br/>");
				writer.writeText("Pas de cours", null);
			}
		}
	}

	private String writeDate(Date date){
		SimpleDateFormat d = new SimpleDateFormat("dd/MM");
		String res = "";
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(date);
		switch (cal.get(Calendar.DAY_OF_WEEK)) {
		case Calendar.MONDAY:
			res += "Lundi";
			break;
		case Calendar.TUESDAY:
			res += "Mardi";
			break;
		case Calendar.WEDNESDAY:
			res += "Mercredi";
			break;
		case Calendar.THURSDAY:
			res += "Jeudi";
			break;
		case Calendar.FRIDAY:
			res += "Vendredi";
			break;
		case Calendar.SATURDAY:
			res += "Samedi";
			break;
		case Calendar.SUNDAY:
			res += "Dimanche";
			break;
		}
		res += ", le " + d.format(date);
		return res;
	}
	
	public String getFamily()
	{
		return "HelloFamily";
	}
}  
